<?php
namespace servers\wjy\EsServer;

class EsServer
{
    protected $client;
    public function __construct()
    {
        $hosts = [
            '127.0.0.1',         // IP + Port
        ];
        $client = ClientBuilder::create()           // Instantiate a new ClientBuilder
        ->setHosts($hosts)      // Set the hosts
        ->build();
        return $this->client = $client;
    }

    /**
     * 添加
     */
    public function EsAdd($indexName,$indexAll)
    {
        $params = [
            'index'=>$indexName ,
            'id'=>$indexAll['id'],
            'body' =>$indexAll
        ];

        $this->client->create($params);
    }

    /**
     * 搜索
     */
    public function searchEs($indexName,$indexAll)
    {
        $params = [
            'index' => $indexName,
            'body'  => [
                'query' => [
                    'match' => [
                        'testField' => $indexAll
                    ]
                ]
            ]
        ];

        return $this->client->search($params);
    }

}